<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单商品
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

//引入字典类
quanli_require('module/base/system/itemHandler.class.php');

//引入订单商品生产表查询类
quanli_include('module/base/bill/billGoodsProduct.class.php');

class pa_warehouseManage {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            global $G_PGLOB;

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->putInStoreList();    //质检入库
                    break;

                case 2:
                    $this->getBusScan();    //上车扫描
                    break;

                case 3:
                    $this->downBusScan();    //下车扫描
                    break;

                case 4:
                    $this->details(); //详情
                    break;

                default:
                    $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');

            }

        }

    }


    /**
     * 质检入库
     */
    private function putInStoreList() {

        global $G_PGLOB;

        //实例化字典类
        $itemHandler = new itemHandler();

        //配送开始时间数组
        $sendStartTime_array = array();

        //配送结束时间数组
        $sendEndTime_array = array();

        //配送时间参数
        $itemStart_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );

        //查询字典表信息
        $temp_array = $itemHandler->searchItem( $itemStart_array );

        if( $temp_array ){
            foreach( $temp_array as $val ){
                if($val['itemCategoryID'] == 9){
                    $sendStartTime_array[]['itemName'] = $val['itemName'];
                }

                if($val['itemCategoryID'] == 10){
                    $sendEndTime_array[]['itemName'] = $val['itemName'];
                }

            }
        }

        //当前时间
        $currentDate = $G_PGLOB->sys_date;
        $G_PGLOB->smarty->assign('currentDate',$currentDate);
        $G_PGLOB->smarty->assign_by_ref('sendStartTime_array',$sendStartTime_array);//配送开始时间
        $G_PGLOB->smarty->assign_by_ref('sendEndTime_array',$sendEndTime_array);//配送结束时间
        $G_PGLOB->smarty->assign('voucher_operator',$G_PGLOB->currentUserID);//质检人
        $G_PGLOB->smarty->assign( 'EDIT_PERMISSION' , $G_PGLOB->has_permission(20001) );
        $G_PGLOB->smarty->assign('userFlg',1);// 1.质检入库 2.上车扫描 3.下车扫描
        $G_PGLOB->smarty->display('production/voucherGoodsList.tpl');

    }


    /**
     * 上车扫描
     */
    private function getBusScan() {

        global $G_PGLOB;

        //实例化字典类
        $itemHandler = new itemHandler();

        //配送开始时间数组
        $sendStartTime_array = array();

        //配送结束时间数组
        $sendEndTime_array = array();

        //配送时间参数
        $itemStart_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );

        //查询字典表信息
        $temp_array = $itemHandler->searchItem( $itemStart_array );

        if( $temp_array ){
            foreach( $temp_array as $val ){
                if($val['itemCategoryID'] == 9){
                    $sendStartTime_array[]['itemName'] = $val['itemName'];
                }

                if($val['itemCategoryID'] == 10){
                    $sendEndTime_array[]['itemName'] = $val['itemName'];
                }

            }
        }

        //当前时间
        $currentDate = $G_PGLOB->sys_date;
        $city = get_city();
        $G_PGLOB->smarty->assign('currentDate',$currentDate);
        $G_PGLOB->smarty->assign_by_ref('sendStartTime_array',$sendStartTime_array);//配送开始时间
        $G_PGLOB->smarty->assign_by_ref('sendEndTime_array',$sendEndTime_array);//配送结束时间
        $G_PGLOB->smarty->assign('city',$city);//查询城市
        $G_PGLOB->smarty->assign('voucher_operator',$G_PGLOB->currentUserID);//上车扫描人
        $G_PGLOB->smarty->assign('userFlg',2);// 1.质检入库 2.上车扫描 3.下车扫描
        $G_PGLOB->smarty->display('production/voucherGoodsList.tpl');

    }

    /**
     * 下车扫描
     */
    private function downBusScan() {

        global $G_PGLOB;

        //实例化字典类
        $itemHandler = new itemHandler();

        //配送开始时间数组
        $sendStartTime_array = array();

        //配送结束时间数组
        $sendEndTime_array = array();

        //配送时间参数
        $itemStart_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );

        //查询字典表信息
        $temp_array = $itemHandler->searchItem( $itemStart_array );

        if( $temp_array ){
            foreach( $temp_array as $val ){
                if($val['itemCategoryID'] == 9){
                    $sendStartTime_array[]['itemName'] = $val['itemName'];
                }

                if($val['itemCategoryID'] == 10){
                    $sendEndTime_array[]['itemName'] = $val['itemName'];
                }

            }
        }

        //保存配送结束时间最晚的时间
        $end_time = '';
        if($sendEndTime_array){
            $result_data = end($sendEndTime_array);
            $end_time = $result_data['itemName'];
        }


        // add by 20141030 allen.qiang cdd  FUN-BK-0016-订单下错站处理 start
        $user_master_sql='select distributionID from user_master where userID=?';
        $master_query=$G_PGLOB->DBCA->stmt_query($user_master_sql,'s',$G_PGLOB->currentUserID);
        $distributionID=$master_query[0]->distributionID;
        $beginTime=date("Y-m-d")." 00:00:00";
        $endTime=date("Y-m-d")." 23:59:59";
        $downBusScan_sql="select * from bill_downBusScan_miss where distribution_id_f in (?) and createTime> ? and createTime < ?  order by createTime asc";
        $log_query=$G_PGLOB->DBCA->stmt_query($downBusScan_sql,'sss',$distributionID,$beginTime,$endTime);
        if(!empty($log_query)){
            foreach($log_query as $k=>$log){
                $user_sql='select trueName from user_master where userID=?';
                $user_info=$G_PGLOB->DBCA->stmt_query($user_sql,'s',$log->creator);
                $dis_sql="SELECT distribution_name FROM sys_distribution where distribution_id=?";
                $dis_info=$G_PGLOB->DBCA->stmt_query($dis_sql,'s',$log->distribution_id_f);
                $dis_info_2=$G_PGLOB->DBCA->stmt_query($dis_sql,'s',$log->distribution_id_t);
                $log_query[$k]->distribution_id_f=$dis_info[0]->distribution_name;
                $log_query[$k]->distribution_id_t=$dis_info_2[0]->distribution_name;
                $log_query[$k]->creator=$user_info[0]->trueName;
            }
        }
        $G_PGLOB->smarty->assign('log_query',$log_query);//查询当天非本站订单操作记录

        // add by 20141030 allen.qiang cdd  FUN-BK-0016-订单下错站处理 end
        //当前时间
        $currentDate = $G_PGLOB->sys_date;
        $city = get_city();
        $G_PGLOB->smarty->assign_by_ref('end_time',$end_time);
        $G_PGLOB->smarty->assign('currentDate',$currentDate);
        $G_PGLOB->smarty->assign_by_ref('sendStartTime_array',$sendStartTime_array);//配送开始时间
        $G_PGLOB->smarty->assign_by_ref('sendEndTime_array',$sendEndTime_array);//配送结束时间
        $G_PGLOB->smarty->assign('city',$city);//查询城市
        $G_PGLOB->smarty->assign('voucher_operator',$G_PGLOB->currentUserID);//质检人
        $G_PGLOB->smarty->assign('userFlg',3);// 1.质检入库 2.上车扫描 3.下车扫描
        $G_PGLOB->smarty->display('production/voucherGoodsList.tpl');

    }


    /**
     *
     * 详情
     */
    public function details(){

        global $G_PGLOB;

        if(!isset($_REQUEST['para'], $_REQUEST['send_date'], $_REQUEST['distribution_id'])){
            $G_PGLOB->smarty->assign('msg', '缺少参数，请审核后再操作');
            $G_PGLOB->smarty->display('comm/error.tpl');
            return;
        }

        //校验配送日期
        $billGoodsProduct = new billGoodsProduct();
        $arg_sendDate= array(
            'send_date'=>$_REQUEST['send_date']
        );
        $billGoodsProductArray = $billGoodsProduct->searchBillGoodsInfo( $arg_sendDate );

        if( !$billGoodsProductArray ){
            $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
            $G_PGLOB->smarty->display('comm/error.tpl');
            return;
        }


        $arg_distribution = array(
            'distribution_id'=>$_REQUEST['distribution_id']
        );
        //校验配送站
        $billGoodsProductInfo = $billGoodsProduct->searchBillGoodsInfo( $arg_distribution );
        if( !$billGoodsProductInfo ){
            $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
            $G_PGLOB->smarty->display('comm/error.tpl');
            return;
        }

        $G_PGLOB->smarty->assign('para',$_REQUEST['para']);
        $G_PGLOB->smarty->assign('send_date',$_REQUEST['send_date']);
        $G_PGLOB->smarty->assign('distribution_id',$_REQUEST['distribution_id']);
        if( isset( $_REQUEST['send_start_time']) && !is_date($_REQUEST['send_start_time'])){
            $G_PGLOB->smarty->assign('send_start_time',$_REQUEST['send_start_time']);
        }
        if( isset( $_REQUEST['send_start_time_end']) && !is_date($_REQUEST['send_start_time_end'])){
            $G_PGLOB->smarty->assign('send_start_time_end',$_REQUEST['send_start_time_end']);
        }
        $G_PGLOB->smarty->display('production/voucherGoodsDetail.tpl');
    }
}